﻿using Goooodbye.Core.Models;
using Goooodbye.Core.Services;
using NLog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace Goooodbye.Infrastructure.Services
{
    public class DictExportService : IExportService<DictModel>
    {
        private static readonly Logger logger = LogManager.GetLogger(Assembly.GetExecutingAssembly().GetName().Name);
        private const string CSV_TOP_ROW = "词条,转换为,备注";

        public int Export(IList<DictModel> list, string fileName)
        {
            int ret = 0;
            if (!String.IsNullOrEmpty(fileName))
            {
                using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.UTF8))
                {
                    sw.WriteLine(CSV_TOP_ROW);
                    foreach (var item in list)
                    {
                        sw.WriteLine($"{item.Term},{item.Mapping},{item.Note}");
                        ret++;
                    }
                }
            }
            logger.Info($"Exported: {ret}");
            return ret;
        }
    }
}
